package fr.inserm.umr915.vcfannotator.bio;

import java.io.File;
import java.io.IOException;

import net.sf.picard.reference.ReferenceSequence;
import net.sf.picard.reference.ReferenceSequenceFile;
import net.sf.picard.reference.ReferenceSequenceFileFactory;

public class SamSequenceProvider implements SequenceProvider
	{
	private ReferenceSequenceFile refSeq=null;
	private File fastaIndexedFile;
	
	public SamSequenceProvider(File fastaIndexedFile)
		{
		this.fastaIndexedFile=fastaIndexedFile;
		}
	
	@Override
	public void close()
		{
		refSeq=null;
		}
	
	@Override
	public GenomicSequence getGenomicSequence(String chrom) throws IOException {
		if(this.refSeq==null)
			{
			refSeq=ReferenceSequenceFileFactory.getReferenceSequenceFile(this.fastaIndexedFile);
			}
		ReferenceSequence seq=this.refSeq.getSequence(chrom);
		return new GenomicSequence(chrom,0, seq.getBases());
		}
	
	@Override
	public GenomicSequence getGenomicSequence(String chrom, int chromStart0,
			int chromEnd0) throws IOException
		{
		if(this.refSeq==null)
			{
			refSeq=ReferenceSequenceFileFactory.getReferenceSequenceFile(this.fastaIndexedFile);
			}
		ReferenceSequence seq=this.refSeq.getSubsequenceAt(chrom, chromStart0-1, chromEnd0-1);
		return new GenomicSequence(chrom, chromStart0, seq.getBases());
		}
	}
